<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>多留言板</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }
        .container {
            display: flex;
            flex-direction: column;
            align-items: center;
            width: 100%;
            max-width: 1200px;
            padding: 20px;
            background: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 10px;
            overflow: hidden;
        }
        .input-section {
            width: 100%;
            max-width: 800px;
            margin-bottom: 20px;
            background: #f9f9f9;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            transition: box-shadow 0.3s ease;
        }
        .input-section:hover {
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
        }
        .board-selector {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;
        }
        .board-selector select {
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            background-color: #f9f9f9;
            color: #333;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        .board-selector select:hover {
            background-color: #e1f5fe;
        }
        .input-section textarea {
            width: 100%;
            max-width: 100%;
            margin-bottom: 10px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 16px;
            color: #333;
            resize: none;
            transition: border-color 0.3s ease;
        }
        .input-section textarea:focus {
            border-color: #007bff;
        }
        .input-section button {
            padding: 10px 20px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s ease;
        }
        .input-section button:hover {
            background-color: #0056b3;
        }
        .boards {
            display: flex;
            justify-content: space-around;
            width: 100%;
            max-width: 1200px;
        }
        .board {
            flex: 1;
            margin: 0 10px;
            background: #f9f9f9;
            border: 1px solid #ccc;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            transition: box-shadow 0.3s ease;
        }
        .board:hover {
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
        }
        .board h2 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }
        .board .messages {
            max-height: 300px;
            overflow-y: auto;
            padding: 10px;
            background-color: #e1f5fe;
            border: 1px solid #b3e5fc;
            border-radius: 10px;
        }
        .board .message {
            padding: 10px;
            margin-bottom: 10px;
            background-color: #e1f5fe;
            border: 1px solid #b3e5fc;
            border-radius: 5px;
            font-size: 14px;
            color: #333;
            transition: background-color 0.3s ease;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .board .message:hover {
            background-color: #d0e8f9;
        }
        .board .message button {
            margin-left: 4px;
            padding: 5px 10px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 12px;
            transition: background-color 0.3s ease;
        }
        .board .message button.delete-button {
            background-color: #ff4d4d;
            color: #fff;
        }
        .board .message button.delete-button:hover {
            background-color: #ff1a1a;
        }
        .board .message button.like-button {
            background: #ffe066;
            color: #333;
        }
        .board .message button.like-button:hover {
            background: #ffd43a;
        }
        .board .messages::-webkit-scrollbar {
            width: 8px;
        }
        .board .messages::-webkit-scrollbar-thumb {
            background-color: #ccc;
            border-radius: 4px;
        }
        .board .messages::-webkit-scrollbar-track {
            background-color: #f1f1f1;
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="input-section">
        <div class="board-selector">
            <label for="boardSelect">选择留言板：</label>
            <select id="boardSelect">
                <option value="board1">留言板1</option>
                <option value="board2">留言板2</option>
                <option value="board3">留言板3</option>
            </select>
        </div>
        <form id="messageForm">
            <textarea id="messageInput" placeholder="请输入留言..." rows="4"></textarea>
            <button type="submit">提交留言</button>
        </form>
    </div>
    <div class="boards">
        <div class="board" id="board1">
            <h2>留言板1</h2>
            <div class="messages" id="messages1"></div>
        </div>
        <div class="board" id="board2">
            <h2>留言板2</h2>
            <div class="messages" id="messages2"></div>
        </div>
        <div class="board" id="board3">
            <h2>留言板3</h2>
            <div class="messages" id="messages3"></div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', () => {
    const messageForm = document.getElementById('messageForm');
    const messageInput = document.getElementById('messageInput');
    const boardSelect = document.getElementById('boardSelect');
    const boards = ['board1', 'board2', 'board3'];

    /* ===== 点赞功能：读/写 localStorage ===== */
    function getLikes(board, idx) {
        const likes = JSON.parse(localStorage.getItem('likes')) || {};
        return likes[board] && likes[board][idx] ? likes[board][idx] : 0;
    }
    function setLike(board, idx, count) {
        const likes = JSON.parse(localStorage.getItem('likes')) || {};
        if (!likes[board]) likes[board] = {};
        likes[board][idx] = count;
        localStorage.setItem('likes', JSON.stringify(likes));
    }

    /* 留言数据（带点赞） */
    const allBoards = JSON.parse(localStorage.getItem('allBoards')) || { board1: [], board2: [], board3: [] };

    /* 渲染留言 */
    function displayMessages() {
        boards.forEach(board => {
            const messagesDiv = document.getElementById(`messages${board.slice(-1)}`);
            const messages = allBoards[board];
            messagesDiv.innerHTML = '';
            messages.forEach((msg, idx) => {
                const div = document.createElement('div');
                div.className = 'message';
                div.innerHTML = `
                    <span>${msg}</span>
                    <div>
                        <button class="like-button" data-board="${board}" data-index="${idx}">
                            点赞 <span class="like-count">${getLikes(board, idx)}</span>
                        </button>
                        <button class="delete-button" data-board="${board}" data-index="${idx}">删除</button>
                    </div>
                `;
                messagesDiv.appendChild(div);
            });
        });
    }

    /* 提交留言 */
    messageForm.addEventListener('submit', e => {
        e.preventDefault();
        const msg = messageInput.value.trim();
        if (msg) {
            const cur = boardSelect.value;
            allBoards[cur].push(msg);
            localStorage.setItem('allBoards', JSON.stringify(allBoards));
            displayMessages();
            messageInput.value = '';
        }
    });

    /* 事件委托：点赞 & 删除 */
    document.addEventListener('click', e => {
        if (e.target.closest('.like-button')) {
            const btn = e.target.closest('.like-button');
            const board = btn.dataset.board;
            const idx   = Number(btn.dataset.index);
            const newCount = getLikes(board, idx) + 1;
            setLike(board, idx, newCount);
            btn.querySelector('.like-count').textContent = newCount;
        }
        if (e.target.classList.contains('delete-button')) {
            const board = e.target.dataset.board;
            const idx   = Number(e.target.dataset.index);
            allBoards[board].splice(idx, 1);
            localStorage.setItem('allBoards', JSON.stringify(allBoards));
            displayMessages();
        }
    });

    displayMessages();
});
</script>
</body>
</html>
